<template>
  <div class="user-search">
    <el-form label-width="80px" :model="searchForm" ref="formRef">
      <el-row :gutter="120">
        <el-col :span="8">
          <el-form-item label="用户名" width="100px" prop="name">
            <el-input placeholder="请输入查询的用户名" v-model="searchForm.name" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="真实姓名" prop="realname">
            <el-input placeholder="请输入查询的真实姓名" v-model="searchForm.realname" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="电话号码" prop="cellphone">
            <el-input placeholder="请输入查询的电话号码" v-model="searchForm.cellphone" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="状态" prop="enable">
            <el-select placeholder="请选择状态" style="width: 100%" v-model="searchForm.enable">
              <el-option label="启用" :value="1" />
              <el-option label="关闭" :value="0" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="创建时间" prop="createAt">
            <el-date-picker
              v-model="searchForm.createAt"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              size="default"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 2.重置和搜索的按钮 -->
    <div class="btns">
      <el-button icon="Refresh" @click="handleResetClick">重置</el-button>
      <el-button icon="Search" type="primary" @click="handleQueryClick">查询</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { ElForm } from 'element-plus'

// 定义自定义事件
const emit = defineEmits(['queryClick', 'resetClick'])

const searchForm = reactive({
  name: '',
  realname: '',
  cellphone: '',
  enable: 1,
  createAt: ''
})

// 重置操作
const formRef = ref<InstanceType<typeof ElForm>>()
function handleResetClick() {
  // 将数据进行重置
  formRef.value?.resetFields()

  // 将时间传递给父组件,通过父组件重新发送网络请求
  emit('resetClick')
}
// 查询操作
function handleQueryClick() {
  // 向父组件发送事件，在父组件上监听
  emit('queryClick', searchForm)
}
</script>

<style lang="less" scoped>
.user-search {
  padding: 20px 30px 0 10px;
  background-color: #fff;

  .btns {
    text-align: right;
    padding: 0 5px 10px 0;

    .el-button {
      height: 36px;
    }
  }
}
</style>
